﻿/**
 * ...
 * @author wangjun
 */
import mx.utils.Delegate
class A2.utils.uMC 
{
	
	public function uMC() 
	{
		
	}
	public static function newMC(_ctnr:MovieClip,scope:MovieClip,onFrame:Function) {
		var ctnr = (_ctnr == null) ? _root : _ctnr;
		var dd = ctnr.getNextHighestDepth();
		var mc = ctnr.createEmptyMovieClip(dd.toString(), dd);
		
		if (onFrame != null && scope!=null) {
			// mc.onEnterFrame = Delegate.Function() {
				 
			//}
			 
			
			 mc.onEnterFrame = Delegate.create(scope, onFrame)
		}
		
		return  mc;
		 
	} 
	public static function playBack(mc:MovieClip):Void {
		mc.onEnterFrame=function(){
			this.gotoAndStop(this._currentframe-1);
			//trace('this._currentframe'+this._currentframe);
			if (this._currentframe == 1) {
				delete this.onEnterFrame;
			} 
		}
	}
	
	public static function delCtrl(mc) {
			delete  mc.onEnterFrame;
			mc.onEnterFrame = null;
			removeMovieClip (mc);
			
	}
	
	public static function orderBy(type, scope):Array {
		var arr:Array = [];
		if (type == '|') {
			for (var i in scope) {
				//if (scope[i] instanceof MovieClip) {
				if (scope[i].constructor == MovieClip) {
					arr.push({mc:scope[i], pos:Math.floor(scope[i]._y)});
				}
			}
		} else {
			for (var i in scope) {
				if (scope[i] instanceof MovieClip) {
					arr.push({mc:scope[i], pos:Math.floor(scope[i]._x)});
				}
			}
		}
		arr.sortOn('pos', Array.NUMERIC);
		//arr.reverse();
		//
		var arr2 = [];
		for (var i = 0; i<arr.length; i++) {
			//trace(arr[i].mc._name+' - '+arr[i].pos)
			arr2.push(arr[i].mc);
		}
		return arr2;
	}
}